package com.daify.threadlearn.chapter01.executorService;

import java.util.concurrent.*;

/**
 *
 * @author daify
 * @date 2019-06-20 10:36
 **/
public class ForkJoinPoolTest {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        int[] arr = new int[100];
        int total = 0;
        //初始化100个数组元素
        for(int i= 0,len = arr.length;i<len;i++){
            //对数组元素赋值，并将数组元素的值添加到sum总和中
            arr[i] = 100;
            total = total + 100;
        }
        ForkJoinPool pool = null;
        try {
            System.out.println("初始化数据总和："+total);
            JoinTask task = new JoinTask(arr,0,arr.length);
            // 创建一个通用池，这个是jdk1.8提供的功能
            pool = ForkJoinPool.commonPool();
            Future<Integer> future = pool.submit(task);
            // 提交分解的SumTask 任务
            System.out.println("多线程执行结果：" + future.get());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (pool != null) {
                pool.shutdown();
            }
        }
    }
}
